//
//  ReceiveAddressViewController.m
//  YunGou
//
//  Created by macbook on 16/5/18.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "ReceiveAddressViewController.h"
#import "AddAddressViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "ReceiveAddressCell.h"
#import "ReceiveAddressModel.h"

@interface ReceiveAddressViewController ()
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation ReceiveAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *rightBarItem = [UIBarButtonItem barButtonTitle:nil image:@"wdshdz_tj" frame:CGRectMake(0, 0, 20, 20) target:self sel:@selector(addReceiveAddress)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
      _dataArray = [NSMutableArray array];
    [NetWorkTool myReceiveAddressWithSuccess:^(id object) {
        if ([object[@"code"] integerValue] == 1) {
            for (NSDictionary *dict in object[@"data"]) {
                ReceiveAddressModel *model = [ReceiveAddressModel modelWithDict:dict];
                [_dataArray addObject:model];
            }
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];

}

- (void)addReceiveAddress{
    AddAddressViewController *addAddressVC = SB(@"Mine", @"AddAddressVC");
    addAddressVC.title = @"添加地址";
    [self.navigationController pushViewController:addAddressVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReceiveAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.receiveAddressModel = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 64;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AddAddressViewController *addAddressVC = SB(@"Mine", @"AddAddressVC");
    addAddressVC.title = @"编辑地址";
    addAddressVC.receiveAddressModel = _dataArray[indexPath.row];
    [self.navigationController pushViewController:addAddressVC animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ReceiveAddressModel *addressModel = _dataArray[indexPath.row];
        [NetWorkTool deleteReceiveAddressWithAddressId:addressModel.Id success:^(id object) {
            if ([object[@"code"] integerValue] == 1) {
                [MBProgressHUD showSuccess:@"删除成功"];
                [_dataArray removeObject:addressModel];
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }else{
                [MBProgressHUD showError:@"删除失败"];
            }
        } failure:^(NSError *error) {
                
        }];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
